<?php

namespace Juheip\IpService\log;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class PlatFormsLogger
{
    private $logger;

    public function __construct(string $name, string $path, string $level = 'info', int $keepDays = 30)
    {
        if (!is_dir($path)) {
            mkdir($path, 0755, true);
        }

        $this->logger = new Logger($name);

        $handler = new RotatingFileHandler($path . $name . '.log', $keepDays);

        $formatter = new LineFormatter(
            "[%datetime%] [%level_name%] %message% %context%\n",
            'Y-m-d H:i:s',
            true,
            true
        );
        
        $handler->setFormatter($formatter);

        $level = constant(Logger::class . '::' . strtoupper($level));
        $handler->setLevel($level);

        $this->logger->pushHandler($handler);
    }

    public function info(string $message, array $context = []): void
    {
        $this->logger->info($message, $context);
    }

    public function error(string $message, array $context = []): void
    {
        $this->logger->error($message, $context);
    }

    public function warning(string $message, array $context = []): void
    {
        $this->logger->warning($message, $context);
    }
}